package gui;

import io.IoHandler;

import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SpringLayout;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.UIManager;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import main.Main;
import main.ProgramOptions;
import main.ProgramOptions.Algorithem;
import main.ProgramOptions.EdgeCount;
import java.awt.Font;
import java.io.File;
/**
 * 
 * @author dannysi
 *
 *The maiin window gui
 */
public class Gui2 {

	public JFrame frame;
	private JTextField inputPath;
	private JTextField outputPath;
	private JTextField numberOfAgents;
	private boolean moreEdges=true;
	public static Gui2 window;

	/**
	 * Launch the application.
	 * This is the MAIN function of the program.
	 */
	public static void main(String[] args) {
		
		IoHandler.inputConfigurationFile();
		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}

		catch (Exception e)
		{
			System.out.println("Unable to load Windows look and feel");
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					window = new Gui2();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Gui2() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 454, 296);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		SpringLayout springLayout = new SpringLayout();
		frame.getContentPane().setLayout(springLayout);
		
		JButton btnDinic = new JButton("Dinic");
		btnDinic.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				DinicGui x= new DinicGui();
				DinicGui.main(null);
				frame.setVisible(false);
				ProgramOptions.algo=Algorithem.DINIC;
				if (moreEdges)
					ProgramOptions.optionEdgeCount=EdgeCount.MORE;
				else
					ProgramOptions.optionEdgeCount=EdgeCount.LESS;
				ProgramOptions.inputFile=inputPath.getText();
				ProgramOptions.outputFile=outputPath.getText();
				IoHandler.outputConfigurationFile();
				try{
					ProgramOptions.numberOfAgents=Integer.parseInt(numberOfAgents.getText());
				}catch (NumberFormatException ex) {
					ProgramOptions.numberOfAgents=4;
				}
				
				
			}
		});
		frame.getContentPane().add(btnDinic);
		
		JButton btnClustering = new JButton("Voltage Clustering");
		springLayout.putConstraint(SpringLayout.WEST, btnClustering, 35, SpringLayout.EAST, btnDinic);
		springLayout.putConstraint(SpringLayout.SOUTH, btnClustering, -83, SpringLayout.SOUTH, frame.getContentPane());
		btnClustering.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.setVisible(false);
				ProgramOptions.algo=Algorithem.CLUSTERING1;
				if (moreEdges)
					ProgramOptions.optionEdgeCount=EdgeCount.MORE;
				else
					ProgramOptions.optionEdgeCount=EdgeCount.LESS;
				
				ProgramOptions.inputFile=inputPath.getText();
				ProgramOptions.outputFile=outputPath.getText();
				ProgramOptions.numberOfAgents=Integer.parseInt(numberOfAgents.getText());
				IoHandler.outputConfigurationFile();
				Main.processClustering();
			}
		});
		frame.getContentPane().add(btnClustering);
		
		inputPath = new JTextField();
		springLayout.putConstraint(SpringLayout.EAST, inputPath, -103, SpringLayout.EAST, frame.getContentPane());
		frame.getContentPane().add(inputPath);
		inputPath.setColumns(10);
		
		outputPath = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, inputPath, 0, SpringLayout.WEST, outputPath);
		frame.getContentPane().add(outputPath);
		outputPath.setColumns(10);
		
		numberOfAgents = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, btnDinic, 56, SpringLayout.SOUTH, numberOfAgents);
		springLayout.putConstraint(SpringLayout.WEST, numberOfAgents, 0, SpringLayout.WEST, inputPath);
		springLayout.putConstraint(SpringLayout.EAST, numberOfAgents, 86, SpringLayout.WEST, inputPath);
		frame.getContentPane().add(numberOfAgents);
		numberOfAgents.setColumns(10);
		numberOfAgents.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				if (e.getKeyChar()<'0' || e.getKeyChar()>'9')
					e.consume();
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		JLabel lblNewLabel = new JLabel("Input Path");
		springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel, 10, SpringLayout.NORTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, inputPath, -3, SpringLayout.NORTH, lblNewLabel);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Output Path");
		springLayout.putConstraint(SpringLayout.WEST, outputPath, 59, SpringLayout.EAST, lblNewLabel_1);
		springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel_1, 23, SpringLayout.SOUTH, lblNewLabel);
		springLayout.putConstraint(SpringLayout.NORTH, outputPath, -3, SpringLayout.NORTH, lblNewLabel_1);
		springLayout.putConstraint(SpringLayout.WEST, lblNewLabel, 0, SpringLayout.WEST, lblNewLabel_1);
		springLayout.putConstraint(SpringLayout.WEST, lblNewLabel_1, 10, SpringLayout.WEST, frame.getContentPane());
		frame.getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Number Of Agents");
		springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel_2, 29, SpringLayout.SOUTH, lblNewLabel_1);
		springLayout.putConstraint(SpringLayout.NORTH, numberOfAgents, -3, SpringLayout.NORTH, lblNewLabel_2);
		springLayout.putConstraint(SpringLayout.WEST, lblNewLabel_2, 0, SpringLayout.WEST, lblNewLabel_1);
		frame.getContentPane().add(lblNewLabel_2);
		
		JButton browseInputBtn = new JButton("Browse...");
		springLayout.putConstraint(SpringLayout.NORTH, browseInputBtn, 7, SpringLayout.NORTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, browseInputBtn, 6, SpringLayout.EAST, inputPath);
		browseInputBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser filechooser=new JFileChooser();
				if (ProgramOptions.lastInputFolder!=null)
					filechooser.setCurrentDirectory(new File(ProgramOptions.lastInputFolder));
				filechooser.showOpenDialog(null);
				try{
				inputPath.setText(filechooser.getSelectedFile().getPath());
				}catch (NullPointerException ex){}
			}
		});
		frame.getContentPane().add(browseInputBtn);
		
		JButton browseOutputBtn = new JButton("Browse...");
		springLayout.putConstraint(SpringLayout.WEST, browseOutputBtn, 341, SpringLayout.WEST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, outputPath, -6, SpringLayout.WEST, browseOutputBtn);
		springLayout.putConstraint(SpringLayout.NORTH, browseOutputBtn, 0, SpringLayout.NORTH, outputPath);
		frame.getContentPane().add(browseOutputBtn);
		
		JPanel panel = new JPanel();
		springLayout.putConstraint(SpringLayout.WEST, panel, 10, SpringLayout.WEST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, panel, -320, SpringLayout.EAST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, btnDinic, 63, SpringLayout.EAST, panel);
		springLayout.putConstraint(SpringLayout.NORTH, panel, 18, SpringLayout.SOUTH, lblNewLabel_2);
		springLayout.putConstraint(SpringLayout.SOUTH, panel, 147, SpringLayout.SOUTH, lblNewLabel_2);
		frame.getContentPane().add(panel);
		SpringLayout sl_panel = new SpringLayout();
		panel.setLayout(sl_panel);
		
		final JRadioButton moreEdgesBtn = new JRadioButton("More Edges");
		sl_panel.putConstraint(SpringLayout.NORTH, moreEdgesBtn, 44, SpringLayout.NORTH, panel);
		panel.add(moreEdgesBtn);
		moreEdgesBtn.doClick();
		moreEdges=true;
		
		final JRadioButton lessEdgesBtn = new JRadioButton("Less Edges");
		lessEdgesBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (!moreEdges)
					lessEdgesBtn.doClick(); 
				if (moreEdges){
					moreEdgesBtn.doClick();
					moreEdges=false;
				}
				
				
			}
		});
		sl_panel.putConstraint(SpringLayout.NORTH, lessEdgesBtn, 6, SpringLayout.SOUTH, moreEdgesBtn);
		panel.add(lessEdgesBtn);
		moreEdgesBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (moreEdges)
					moreEdgesBtn.doClick();
				if (!moreEdges){
					lessEdgesBtn.doClick();
					moreEdges=true;
				}
			}
		});
		
		JLabel lblMainGraphStyle = new JLabel("Main Graph Style");
		lblMainGraphStyle.setFont(new Font("Tahoma", Font.PLAIN, 14));
		sl_panel.putConstraint(SpringLayout.NORTH, lblMainGraphStyle, 10, SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.WEST, lblMainGraphStyle, 0, SpringLayout.WEST, moreEdgesBtn);
		panel.add(lblMainGraphStyle);
		
		JButton btnClustering_1 = new JButton("Edge Betweenness Clustering");
		btnClustering_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.setVisible(false);
				ProgramOptions.algo=Algorithem.CLUSTERING2;
				if (moreEdges)
					ProgramOptions.optionEdgeCount=EdgeCount.MORE;
				else
					ProgramOptions.optionEdgeCount=EdgeCount.LESS;
				
				ProgramOptions.inputFile=inputPath.getText();
				ProgramOptions.outputFile=outputPath.getText();
				ProgramOptions.numberOfAgents=Integer.parseInt(numberOfAgents.getText());
				Main.processEBclustering();
				IoHandler.outputConfigurationFile();
			}
		});
		springLayout.putConstraint(SpringLayout.NORTH, btnClustering_1, 6, SpringLayout.SOUTH, btnClustering);
		springLayout.putConstraint(SpringLayout.WEST, btnClustering_1, 0, SpringLayout.WEST, btnClustering);
		frame.getContentPane().add(btnClustering_1);
		browseOutputBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser filechooser=new JFileChooser();
				
				System.out.println(ProgramOptions.lastOutputFolder);
				if (ProgramOptions.lastOutputFolder!=null)
					filechooser.setCurrentDirectory(new File(ProgramOptions.lastOutputFolder));
				filechooser.showSaveDialog(null);
				try{
					outputPath.setText(filechooser.getSelectedFile().getPath());
				}catch (NullPointerException ex){}
				
			}
		});
	}
}
